<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :destroyOnClose="true"
    cancelText="关闭">
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>

          <p-col  :span="24">
            <p-form-item label="父层级" :labelCol="labelCol" :wrapperCol="wrapperCol">


              <p-input v-decorator="[ 'pid', validatorRules.pid]" placeholder="请选择父层级" disabled></p-input>
          </p-form-item>
        </p-col>

          <p-col  :span="24">
            <p-form-item label="产品类别层级编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productHierarchyCode', validatorRules.productHierarchyCode]" placeholder="请输入产品类别层级编码"></p-input>
          </p-form-item>
        </p-col>

          <p-col  :span="24">
            <p-form-item label="产品层级名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productHierarchyName', validatorRules.productHierarchyName]" placeholder="请输入产品层级名称"></p-input>
          </p-form-item>
        </p-col>

          <p-col  :span="24">
            <p-form-item label="产品层级描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'productHierarchyDescribe', validatorRules.productHierarchyDescribe]" placeholder="请输入产品层级描述"></p-input>
          </p-form-item>
        </p-col>

          <p-col  :span="24">
            <p-form-item label="产品层级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['productHierarchy', validatorRules.productHierarchy]" disabled :trigger-change="true" dictCode="product_hierarchy" placeholder="请选择产品层级"/>
          </p-form-item>
        </p-col>

          <p-col  :span="24">
            <p-form-item label="产品类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['productType', validatorRules.productType]" :trigger-change="true" dictCode="produce_type" placeholder="请选择产品类别"/>
          </p-form-item>
        </p-col>
        </p-row>

      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { SubmitOnchangMixin } from '@/mixins/SubmitOnchangMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JTreeSelect from '@/components/jeecg/JTreeSelect'
  
  export default {
    name: "MesQcProductTypeModal",
	mixins: [SubmitOnchangMixin],
    components: { 
      JDictSelectTag,
      JTreeSelect
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '操作',
        width: 800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
          pid: {rules: [
          ]},
          productHierarchyCode: {rules: [
            {required: true, message: '请输入产品类别层级编码!'},
          ]},
          productHierarchyName: {rules: [
            {required: true, message: '请输入产品层级名称!'},
          ]},
          productHierarchyDescribe: {rules: [
            {required: true, message: '请输入产品层级描述!'},
          ]},
          productHierarchy: {rules: [
            {required: true, message: '请输入产品层级!'},
          ]},
          productType: {rules: [
            {required: true, message: '请输入产品类别!'},
          ]},
        },
        url: {
          add: '/mes-quality/mesQcProductType/add',
          edit: '/mes-quality/mesQcProductType/edit',
        },
        expandedRowKeys:[],
        pidField: 'pid'
     
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'pid','productHierarchyCode','productHierarchyName','productHierarchyDescribe','productHierarchy','productType','createTime','createBy','updateTime','updateBy'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
		handleOk () {
			const that = this;
			// 触发表单验证
			this.form.validateFields((err, values) => {
			  if (!err) {
				  that.confirmLoading = true;
				  let httpurl = '';
				  let method = '';
				  let formData = Object.assign(this.model, values);
				  this.formData = Object.assign(this.model, values);
				  //新增触发
				  if(!this.model.id){
					httpurl+=this.url.add;
					method = 'post';
					if(this.EnhanceJS.afterAdd){
					  //新增之前
					  let Obj = eval(this.EnhanceJS.afterAdd)
					  new Obj(this)
					}else{
					  console.log("表单提交数据",formData)
					  that.modalFrom(httpurl,formData,method)
					}
				  }else{
					//编辑触发
					httpurl+=this.url.edit;
					method = 'put';
					if(this.EnhanceJS.afterEdit){
					 //编辑之前
					  let Obj = eval(this.EnhanceJS.afterEdit)
					  new Obj(this)
					}else{
					  console.log("表单提交数据",formData)
					  that.modalFrom(httpurl,formData,method)
					}
				  }
			  }

			})
		},
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'pid','productHierarchyCode','productHierarchyName','productHierarchyDescribe','productHierarchy','productType','createTime','createBy','updateTime','updateBy'))
      },
      submitSuccess(formData,flag){
        if(!formData.id){
          let treeData = this.$refs.treeSelect.getCurrTreeData()
          this.expandedRowKeys=[]
          this.getExpandKeysByPid(formData[this.pidField],treeData,treeData)
          this.$emit('ok',formData,this.expandedRowKeys.reverse());
        }else{
          this.$emit('ok',formData,flag);
        }
      },
      getExpandKeysByPid(pid,arr,all){
        if(pid && arr && arr.length>0){
          for(let i=0;i<arr.length;i++){
            if(arr[i].key==pid){
              this.expandedRowKeys.push(arr[i].key)
              this.getExpandKeysByPid(arr[i]['parentId'],all,all)
            }else{
              this.getExpandKeysByPid(pid,arr[i].children,all)
            }
          }
        }
      }
      
      
    }
  }
</script>
